package com.zcoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcoj.core.model.PageData;
import com.zcoj.core.model.PageWrap;
import com.zcoj.mapper.GroupRegisterMapper;
import com.zcoj.pojo.Dto.GroupRegister;
import com.zcoj.service.GroupRegisterService;
import com.zcoj.util.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author chenbin
 * @version 1.0
 * @description: TODO
 * @date 2023/4/22 22:49
 */
@Service
public class GroupRegisterServiceImpl implements GroupRegisterService {
 @Autowired
 private GroupRegisterMapper groupRegisterMapper;

 @Override
 public Integer create(GroupRegister groupRegister) {
  groupRegisterMapper.insert(groupRegister);
  return groupRegister.getId();
 }

 @Override
 public void deleteById(Integer id) {
  groupRegisterMapper.deleteById(id);
 }

 @Override
 public void deleteByIdInBatch(List<Integer> ids) {
  if (CollectionUtils.isEmpty(ids)) return;
  groupRegisterMapper.deleteBatchIds(ids);
 }

 @Override
 public void updateById(GroupRegister groupRegister) {
  groupRegisterMapper.updateById(groupRegister);
 }

 @Override
 public void updateByIdInBatch(List<GroupRegister> groupRegisters) {
  if (CollectionUtils.isEmpty(groupRegisters)) return;
  for (GroupRegister groupRegister: groupRegisters) {
   this.updateById(groupRegister);
  }
 }

 @Override
 public GroupRegister findById(Integer id) {
  return groupRegisterMapper.selectById(id);
 }

 @Override
 public GroupRegister findOne(GroupRegister groupRegister) {
  Wrapper<GroupRegister> wrapper = new QueryWrapper<>(groupRegister);
  return groupRegisterMapper.selectOne(wrapper);
 }

 @Override
 public List<GroupRegister> findList(GroupRegister groupRegister) {
  Wrapper<GroupRegister> wrapper = new QueryWrapper<>(groupRegister);
  return groupRegisterMapper.selectList(wrapper);
 }

 @Override
 public PageData<GroupRegister> findPage(PageWrap<GroupRegister> pageWrap) {
  IPage<GroupRegister> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
  QueryWrapper<GroupRegister> queryWrapper = new QueryWrapper<>(WrapperUtil.blankToNull(pageWrap.getModel()));
  for(PageWrap.SortData sortData: pageWrap.getSorts()) {
   if (sortData.getDirection().equalsIgnoreCase("DESC")) {
    queryWrapper.orderByDesc(sortData.getProperty());
   } else {
    queryWrapper.orderByAsc(sortData.getProperty());
   }
  }
  return PageData.from(groupRegisterMapper.selectPage(page, queryWrapper));
 }

 @Override
 public long count(GroupRegister groupRegister) {
  Wrapper<GroupRegister> wrapper = new QueryWrapper<>(groupRegister);
  return groupRegisterMapper.selectCount(wrapper);
 }
}
